<template>
	<view class="container padding-top-sm">
		<view
			class="margin-left-sm round margin-right-sm flex justify-between align-center bg-color tabs text-df text-white">
			<view class="tab round text-center" :class="{'on':deliverType==1}" @click="changedeliver(1)">
				自提
			</view>
			<view class="tab round text-center" :class="{'on':deliverType==2}" @click="changedeliver(2)">
				快递
			</view>
			<view class="tab round text-center" :class="{'on':deliverType==3}" @click="changedeliver(3)">
				跑腿
			</view>
		</view>
		<view class="" v-if="type==1">
			<view class="" v-for="items in list">
				<view class="" v-if="deliverType==1">
					<view
						class="flex justify-start align-center radius10 bg-white padding-sm outlets bg-white margin-sm">
						<image :src="items.shopLogo" mode="" class="cu-avatar round"></image>
						<view class="margin-left" style="width: calc(100% - 104rpx)">
							<view class="text-15 text-333">
								{{ items.shopName }}
							</view>
							<view class="text-cut margin-top-sm text-sm text-666">
								{{ items.shopAddress }}
							</view>
						</view>
					</view>
					<view class="margin-left-sm margin-right-sm margin-bottom-sm padding-sm radius10 bg-white">
						<view class="flex justify-start margin-top-sm" v-for="item in items.carDtoList">
							<image :src="item.productImg" mode=""
								style="width: 166rpx;height: 166rpx;min-width: 166rpx;">
							</image>
							<view class="margin-left-sm flex-treble">
								<view class="text-13 text-333 flex justify-between">

									<view class="text-cut-2" style="width: 300rpx;max-width: 300rpx;">
										{{item.productName}}
									</view>
									<view class="flex flex-direction align-end">
										<!-- <view class="" v-if="item.groupStatus">
										¥ {{parseFloat(item.groupPrice/100).toFixed(2)}}
									</view> -->
										<view class="">
											¥ {{parseFloat(item.productPrice/100).toFixed(2)}}
										</view>
										<view class="text-999 margin-top-xs">
											×{{item.buyNum}}
										</view>
									</view>
								</view>
								<view class="text-sm text-999 margin-top">
									{{item.attributes}}
								</view>
							</view>
						</view>
						<view class="flex justify-end align-center margin-top-sm">
							<view class="text-999 text-sm">
								共{{items.productNum}}件
							</view>
							<view class="margin-left flex justify-start align-center text-13">
								<view class="text-333">
									小计
								</view>
								<view class="color">
									¥ {{parseFloat(items.productAmount/100).toFixed(2)}}
								</view>
							</view>
						</view>
					</view>
					<view @click="showModal(items)"
						class="flex justify-between align-center margin-sm padding-sm radius10 bg-white">
						<view class="flex justify-start align-center">
							<image :src="baseUrl + '/static/Coupon.png'" mode="" style="width: 56rpx;height: 56rpx;">
							</image>
							<view class="margin-left-sm text-181818 text-df">
								优惠券
							</view>
						</view>
						<view class="flex justify-start align-center">
							<view class="text-sm text-999">
								{{items.couponHistoryName}}
							</view>
							<view class="cuIcon-right xl margin-left-sm">

							</view>
						</view>
					</view>
				</view>
				<view v-else>
					<view class="radius10 bg-white padding-sm outlets bg-white margin-sm"
						@click="toPage('/filePage/pages/address-list/address-list?is_change=true')">

						<view class="" v-if="userAddress">
							<view class="text-15 text-333">
								{{ userAddress.receiver }}
							</view>
							<view class="text-cut margin-top-sm text-sm text-666">
								{{ userAddress.addressInfo }}
							</view>
						</view>
						<view class="" v-else>
							请选择默认地址
						</view>
					</view>
					<view class="margin-sm padding-sm radius10 bg-white">
						<view class="flex justify-start" v-for="item in items.carDtoList">
							<image :src="item.productImg" mode=""
								style="width: 166rpx;height: 166rpx;min-width: 166rpx;">
							</image>
							<view class="margin-left-sm flex-treble">
								<view class="text-13 text-333 flex justify-between">

									<view class="text-cut-2" style="width: 300rpx;max-width: 300rpx;">
										{{item.productName}}
									</view>
									<view class="flex flex-direction align-end">
										<!-- <view class="" v-if="item.groupStatus">
										¥ {{parseFloat(item.groupPrice/100).toFixed(2)}}
									</view> -->
										<view class="">
											¥ {{parseFloat(item.productPrice/100).toFixed(2)}}
										</view>
										<view class="text-999 margin-top-xs">
											×{{item.buyNum}}
										</view>
									</view>
								</view>
								<view class="text-sm text-999 margin-top">
									{{item.attributes}}
								</view>
							</view>
						</view>
						<view class="flex justify-end align-center margin-top-sm">
							<view class="text-999 text-sm">
								共{{items.productNum}}件
							</view>
							<view class="margin-left flex justify-start align-center text-13">
								<view class="text-333">
									小计
								</view>
								<view class="color">
									¥ {{parseFloat(items.productAmount/100).toFixed(2)}}
								</view>
							</view>
						</view>
					</view>
					<view @click="showModal(items)"
						class="flex justify-between align-center margin-sm padding-sm radius10 bg-white">
						<view class="flex justify-start align-center">
							<image :src="baseUrl + '/static/Coupon.png'" mode="" style="width: 56rpx;height: 56rpx;">
							</image>
							<view class="margin-left-sm text-181818 text-df">
								优惠券
							</view>
						</view>
						<view class="flex justify-start align-center">
							<view class="text-sm text-999">
								{{items.couponHistoryName}}
							</view>
							<view class="cuIcon-right xl margin-left-sm">

							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="" v-else>
			<!-- <view class="" v-for="items in list"> -->
			<!-- <view class="" > -->
			<view v-if="deliverType==1"
				class="flex justify-start align-center radius10 bg-white padding-sm outlets bg-white margin-sm">
				<image :src="shop.logo" mode="" class="cu-avatar round"></image>
				<view class="margin-left" style="width: calc(100% - 104rpx)">
					<view class="text-15 text-333">
						{{ shop.shopName }}
					</view>
					<view class="text-cut margin-top-sm text-sm text-666">
						{{ shop.address }}
					</view>
				</view>
			</view>
			<view v-else class="radius10 bg-white padding-sm outlets bg-white margin-sm"
				@click="toPage('/filePage/pages/address-list/address-list?is_change=true')">

				<view class="" v-if="userAddress">
					<view class="text-15 text-333">
						{{ userAddress.receiver }}
					</view>
					<view class="text-cut margin-top-sm text-sm text-666">
						{{ userAddress.addressInfo }}
					</view>
				</view>
				<view class="" v-else>
					请选择默认地址
				</view>
			</view>
			<view class="margin-left-sm margin-right-sm margin-bottom-sm padding-sm radius10 bg-white">
				<view class="flex justify-start margin-top-sm" v-for="item in info.carDtoList">
					<image :src="item.productImg" mode="" style="width: 166rpx;height: 166rpx;min-width: 166rpx;">
					</image>
					<view class="margin-left-sm flex-treble">
						<view class="text-13 text-333 flex justify-between">

							<view class="text-cut-2" style="width: 300rpx;max-width: 300rpx;">
								{{item.productName}}
							</view>
							<view class="flex flex-direction align-end">
								<!-- <view class="" v-if="item.groupStatus">
												¥ {{parseFloat(item.groupPrice/100).toFixed(2)}}
											</view> -->
								<view class="">
									¥ {{parseFloat(item.productPrice/100).toFixed(2)}}
								</view>
								<view class="text-999 margin-top-xs">
									×{{item.buyNum}}
								</view>
							</view>
						</view>
						<view class="text-sm text-999 margin-top">
							{{item.attributes}}
						</view>
					</view>
				</view>
				<view class="flex justify-end align-center margin-top-sm">
					<view class="text-999 text-sm">
						共{{info.productNum}}件
					</view>
					<view class="margin-left flex justify-start align-center text-13">
						<view class="text-333">
							小计
						</view>
						<view class="color">
							¥ {{parseFloat(info.productAmount/100).toFixed(2)}}
						</view>
					</view>
				</view>
			</view>
			<view @click="showModal(info)"
				class="flex justify-between align-center margin-sm padding-sm radius10 bg-white">
				<view class="flex justify-start align-center">
					<image :src="baseUrl + '/static/Coupon.png'" mode="" style="width: 56rpx;height: 56rpx;">
					</image>
					<view class="margin-left-sm text-181818 text-df">
						优惠券
					</view>
				</view>
				<view class="flex justify-start align-center">
					<view class="text-sm text-999">
						{{couponHistoryName}}
					</view>
					<view class="cuIcon-right xl margin-left-sm">

					</view>
				</view>
			</view>
			<!-- </view> -->
			<!-- </view> -->
		</view>

		<view class="margin-sm padding-sm radius10 bg-white">
			<view class="text-181818 text-df">
				选择支付方式
			</view>
			<radio-group class="width-sub margin-top" @change="RadioChange">
				<view class="flex justify-between align-center">
					<view class="title text-13 flex justify-start align-center">
						<image :src="baseUrl + '/static/we-chat-pay.png'" mode="" style="width: 32rpx;height: 32rpx;">
						</image>
						<view class="">
							微信支付
						</view>
					</view>
					<radio class="radio yellow" :class="radio=='A'?'bg-white':''" :checked="radio=='A'?true:false"
						value="A"></radio>
				</view>
				<view class="flex justify-between align-center margin-top-sm">
					<view class="title text-13 flex justify-start align-center">
						<image :src="baseUrl + '/static/balance.png'" mode="" style="width: 32rpx;height: 32rpx;">
						</image>
						<view class="">
							账户余额支付
						</view>
					</view>
					<radio class="radio yellow" :class="radio=='B'?'bg-white':''" :checked="radio=='B'?true:false"
						value="B"></radio>
				</view>
			</radio-group>
		</view>
		<view class="height100">

		</view>
		<view class="c-o-fooder flex justify-end align-center bg-white">
			<view class="flex justify-start align-center">
				<view class="text-df text-999">
					共{{productNum}}件
				</view>
				<view class="margin-left flex justify-start align-center text-15">
					<view class="text-333">
						总计
					</view>
					<view class="color">
						¥ {{parseFloat(realAmount/100).toFixed(2)}}
					</view>
				</view>
			</view>
			<button @click="submit" class="text-15 text-white round margin-left bg-to-right">
				去支付
			</button>
		</view>
		<view class="cu-modal bottom-modal" :class="modalName == 'bottomModal' ? 'show' : ''" @click="hideModal()">
			<view class="cu-dialog">
				<view class="cu-bar bg-white justify-between">
					<view class="action text-green"></view>
					<view class="text-221815 text-17"> 选择优惠券 </view>
					<view class="action text-blue" @tap="hideModal">取消</view>
				</view>
				<scroll-view scroll-y="true" style="height: 200rpx">
					<view class="flex justify-between align-center padding solid-bottom" v-for="item in couponList"
						@click="cheacked(item)">
						<view class="flex justify-between align-center f-grow1">
							<view class="text-xl text-221815">
								{{ item.couponName }}
							</view>
							<!-- <view class="text-sm text-999">
								¥{{ parseFloat(item.couponAmount/100).toFixed(2)}}
							</view> -->
						</view>
						<image v-if="couponHistoryId == item.id" :src="baseUrl + '/static/selected.png'" mode=""
							class="margin-left margin-right-sm" style="width: 26rpx; height: 26rpx"></image>
						<image v-else :src="baseUrl + '/static/unselected.png'" mode=""
							class="margin-left margin-right-sm" style="width: 26rpx; height: 26rpx"></image>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				radio: 'A',
				cartIds: [],
				shopId: '',
				type: '',
				list: [{
					carDtoList: [],
					couponList: [],
					useCoupon: {},
					userAddress: {}
				}],
				info: {},
				userAddress: {},
				shop: {},
				deliverType: 1,
				items: '',
				couponList: [],
				modalName: null,
				buyNowProductInfo: {},
				productNum: 0,
				realAmount: 0,
				deliverTypeParam: [],
				couponHistoryId: '',
				couponHistoryName: '',
				groupId: '',
				memberCount: '',
				groupStatus:'',
				nowBuy:0
			};
		},
		onLoad(options) {
			console.log(options)
			let that = this
			if (options.nowBuy) this.nowBuy = options.nowBuy
			if (options.groupStatus) this.groupStatus = options.groupStatus
			if (options.groupId) this.groupId = options.groupId
			if (options.memberCount) this.memberCount = options.memberCount
			
			if (options.cartIds) {
			
				this.cartIds = JSON.parse(options.cartIds)
					console.log(this.cartIds)
				this.cartIds.forEach(val => {
					that.deliverTypeParam.push({
						cartIds: val.carts,
						deliverType: this.deliverType,
						shopId: val.shopId
					})
				})
			}
			if (options.productNum) this.productNum = options.productNum
			if (options.realAmount) this.realAmount = options.realAmount
			if (options.buyNowProductInfo) this.buyNowProductInfo = JSON.parse(options.buyNowProductInfo)
			if (options.shopId) {
				this.shopId = options.shopId

			}
			if (options.type) {
				// console.log(options.type)
				this.type = options.type
			}

		},
		onShow() {
			if (!uni.getStorageSync('isBack')) {
				this.getBuyInfo()
				if (this.type != 1) {
					this.getShop()
				}

			}
		},
		onUnload() {
			uni.setStorageSync('isBack', false)
		},
		methods: {
			showModal(e) {
				// console.log(e)
				if (e.couponList.length == 0) {
					return
				}
				this.couponList = e.couponList
				this.items = e
				this.modalName = "bottomModal";
			},
			hideModal() {
				this.modalName = null;
			},
			cheacked(e) {
				let that = this
                    this.couponHistoryId = e.id
					this.couponHistoryName = e.couponName
				if (this.type != 2) {
					that.deliverTypeParam.forEach(val => {
						if (that.items.shopId == val.shopId) {
							val.couponHistoryId = e.id
						}
					})
				}

				this.getBuyInfo()
			},
			toPage(url) {
				this.$util.openNewPage(url)
			},
			RadioChange(e) {
				this.radio = e.detail.value
			},
			async getBuyInfo() {
				// console.log(this.cartIds)
				let that = this
				let param
				let url
				if (this.type == 1) {
					url = this.$api.order_prepareList
					param = this.deliverTypeParam
					// if (this.couponHistoryId) {
					// 	param = {
					// 		cartIds: this.cartIds,
					// 		deliverType: this.deliverType,
					// 		shopId: this.shopId,
					// 		couponHistoryId: this.couponHistoryId
					// 	}
					// } else {
					// 	param = {
					// 		cartIds: this.cartIds,
					// 		deliverType: this.deliverType,
					// 		shopId: this.shopId
					// 	}
					// }
				} else {
					if(this.type==3&&this.groupStatus==1&&this.nowBuy==0){
						// console.log('```````````')
						url = this.$api.groupBuy_prepay
					} else {
						url = this.$api.order_prepare
						// console.log('***********')
						
					}
					
					
					if (this.couponHistoryId) {
						param = {
							buyNowProductInfo: this.buyNowProductInfo,
							couponHistoryId: this.couponHistoryId
						}
					} else {
						param = {
							buyNowProductInfo: this.buyNowProductInfo
						}
					}
				}

				// console.log(JSON.stringify(this.buyNowProductInfo))
				const [err, res] = await this.$jsonPost(url, param)
				if (res) {
				  
					
					if (this.type == 1) {
						this.list = res.data.orderProductPrepareRespList
						// this.userAddress = res.data.userAddress
						
						// console.log('*********',uni.getStorageSync('address'))
						if(uni.getStorageSync('address')){
							this.userAddress = uni.getStorageSync('address')
						}else{
							this.userAddress = res.data.userAddress
						}
						if (this.deliverType != 1) {
							that.deliverTypeParam.forEach(val => {
								val.userAddressId = that.userAddress.id

							})
						}
						this.list.forEach(val => {
							if(val.couponHistoryId){
								val.couponHistoryName = val.useCoupon.couponName
							}else{
								if (val.couponList.length == 0) {
								val.couponHistoryName = '无可用优惠券'
							} else {
								val.couponHistoryName = '选择优惠券'
							}
							}
							
						})
					} else {
						this.info = res.data
						
						// console.log('*********',uni.getStorageSync('address'))
						if(uni.getStorageSync('address')){
							this.userAddress = uni.getStorageSync('address')
						}else{
							this.userAddress = res.data.userAddress
						}
						// console.log(res.data)
						// console.log('*********')
						this.productNum = res.data.productNum
						this.realAmount = res.data.realAmount
						if (res.data.couponList.length == 0) {
							this.couponHistoryName = '无可用优惠券'
						} else {
							this.couponHistoryName = '选择优惠券'
						}
					}


					// console.log(res)
				}
			},
			async getShop() {
				const [err, res] = await this.$get(this.$api.shop_detail, {
					id: this.shopId,
				});
				if (res) {
					this.shop = res.data;
					// console.log(res)
				}
			},
			changedeliver(e) {
				this.deliverType = e
				this.deliverTypeParam.forEach(val => {
					val.deliverType = e

				})
				this.getBuyInfo()
			},
			async submit() {
				// console.log(this.cartIds)
				let that = this
				let url
				let param
				if (this.deliverType != 1 && this.userAddress == null) {
					return this.$util.msg('请选择地址')
				}
				if (this.type == 1) {
					url = this.$api.order_createList
					param = this.deliverTypeParam
					// if (this.couponHistoryId) {
					// 	if (this.deliverType == 1) {
					// 		param = {
					// 			buyNowProductInfo: {
					// 				...this.buyNowProductInfo,

					// 			},
					// 			couponHistoryId: this.couponHistoryId,
					// 			deliverType: this.deliverType
					// 		}
					// 	} else {
					// 		param = {
					// 			buyNowProductInfo: {
					// 				...this.buyNowProductInfo,

					// 			},
					// 			couponHistoryId: this.couponHistoryId,
					// 			deliverType: this.deliverType,
					// 			userAddressId: this.info.userAddress.id
					// 		}
					// 	}

					// } else {
					// 	if (this.deliverType == 1) {
					// 		param = {
					// 			buyNowProductInfo: this.buyNowProductInfo,
					// 			deliverType: this.deliverType
					// 		}
					// 	} else {
					// 		param = {
					// 			buyNowProductInfo: this.buyNowProductInfo,
					// 			deliverType: this.deliverType,
					// 			userAddressId: this.info.userAddress.id
					// 		}
					// 	}

					// }
				} else {
					
						// console.log(this.type==3&&this.nowBuy==1)
					if (this.type==3&&this.nowBuy==0) {
						url = this.$api.groupBuy_create
						param = {
							buyNowProductInfo: this.buyNowProductInfo,
							memberCount: this.memberCount,
							groupId: this.groupId
						}
						
					} else {
						url = this.$api.order_create
						if (this.couponHistoryId) {
							if (this.deliverType == 1) {
								param = {
									// cartIds: this.cartIds,
									buyNowProductInfo: {
													...this.buyNowProductInfo,
									
												},
									deliverType: this.deliverType,
									shopId: this.shopId,
									couponHistoryId: this.couponHistoryId
								}
							} else {
								param = {
									// cartIds: this.cartIds,
									buyNowProductInfo: {
													...this.buyNowProductInfo,
									
												},
									deliverType: this.deliverType,
									shopId: this.shopId,
									couponHistoryId: this.couponHistoryId,
									userAddressId: this.userAddress.id
								}
							}
						
						} else {
							if (this.deliverType == 1) {
								param = {
									// cartIds: this.cartIds,
									buyNowProductInfo: {
													...this.buyNowProductInfo,
									
												},
									deliverType: this.deliverType,
									shopId: this.shopId
								}
							} else {
								param = {
									// cartIds: this.cartIds,
									buyNowProductInfo: {
													...this.buyNowProductInfo,
									
												},
									deliverType: this.deliverType,
									shopId: this.shopId,
									userAddressId: this.userAddress.id
								}
							}
						
						}

					}


				}
				// console.log(url)
				// console.log(param)
				const [err, res] = await this.$jsonPost(url, param)
				if (res) {
					// this.info = res.data
					// console.log(res)
					// uni.clearStorage('address')
					uni.removeStorageSync('address')
					this.toPage('/filePage/pages/confirm-payment/confirm-payment?payType=' + this.radio +
						'&realAmount=' + res.data.realAmount + '&promoteAmount=' + res.data.couponAmount +
						'&orderId=' + res.data.id + '&type=' + this.type + '&groupStatus=' + this.groupStatus)

				}



			}
		}
	}
</script>

<style lang="scss" scoped>
	.tabs {
		padding: 4rpx;

		.tab {
			width: 350rpx;
			padding: 16rpx;
		}

		.on {
			background: #fff;
			color: #ED7200;
			font-weight: bold;
		}
	}

	.c-o-fooder {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 10rpx 20rpx;

		button {
			width: 200rpx;
			height: 80rpx;
			line-height: 80rpx;
		}
	}
</style>
